import com.mochimedia.ui.*;

/**
* ...
* @author $(DefaultUser)
*/

class com.mochimedia.ui.TabGroup extends Cell {
    
    private var _type:String = "tabgroup";
    
    private var _textLabels:Array;
    private var _tabs:Object;
    private var _tabsHeight:Number;
    
    private var _defaultTabIndex:Number;
    private var _activeTab:BButton;
    
    public function get activeTab():BButton { return _activeTab; }

    public function get tabs():Object { return _tabs; }
    
    
    //
    //
    public function TabGroup (container:MovieClip, textLabels:Array, defaultTabIndex:Number, height:Number, position:Position, style:Style) {
        
        init(container, textLabels, defaultTabIndex, height, position, style);
        
        if (_container != null) create();
        
    }
    
    //
    //
    private function init (container:MovieClip, textLabels:Array, defaultTabIndex:Number, height:Number, position:Position, style:Style):Void {
        
        super.init(container, 0, 0, false, false, 0, position, style);

        _textLabels = textLabels;
        _defaultTabIndex = defaultTabIndex;
        _tabsHeight = height;
        _tabs = [];

    }
    
    //
    //
    public function create ():Void {
        
        super.create();
        
        if (_parentCell != null) {
            _width = _parentCell.width;
        }
        
        var i:Number;
        
        var tabsPos:Position = new Position(null, Position.ALIGN_LEFT, Position.PLACEMENT_FLOAT, Position.CLEAR_NONE, 0);
       
        for (i = 0; i < _textLabels.length; i++) {
            
            _tabs[i] = _tabs[_textLabels[i]] = (new BButton(null, _textLabels[i], Position.ALIGN_CENTER, NaN, _tabsHeight, true, true, false, tabsPos, _style));
            addChild(BButton(_tabs[i]));
            
        }
        
        _height = 0;
        
        for (i = 0; i < _tabs.length; i++) {
            
            BButton(_tabs[i]).addEventListener(EVENT_CLICK, this, activateTab);
            _height = Math.max(_height, BButton(_tabs[i]).height);
            
        }
        
        
        _width = _parentCell.width - _position.margin_left - _position.margin_right;
        _height = _parentCell.height - _position.margin_top - _position.margin_bottom;

        Position.arrangeContent(this);
		
		_height = Position.getCellContentHeight(this);
        
        if (_defaultTabIndex != undefined) {
            BButton(_tabs[_defaultTabIndex]).select();
            _activeTab = BButton(_tabs[_defaultTabIndex]);
        }
        
    }
    
    //
    //
    public function activateTab (e:Event):Void {
        
        for (var i:Number = 0; i < _tabs.length; i++) if (_tabs[i] != e.target) BButton(_tabs[i]).deselect();
        
        _activeTab = BButton(e.target);

        _activeTab.select();
        
        dispatchEvent(EVENT_CLICK);
        
    }
    
    //
    //
    public function getTabByLabel (textLabel:String):BButton {
        
        for (var i:Number = 0; i < _tabs.length; i++) if (BButton(_tabs[i]).textLabel == textLabel) return BButton(_tabs[i]);

        return null;
        
    }
    
    //
    //
    public function select (tab:Object):Void {
        
        var tabIndex:String = String(tab);

        for (var tabRef:String in _tabs) if (tabRef != tabIndex) BButton(_tabs[tabRef]).deselect();
        
        _activeTab = BButton(_tabs[tabIndex]);
        
        _activeTab.select();
        
        dispatchEvent(EVENT_CLICK);     
        
    }
    
}